package com.yjtc.cbg.commonutildemo.View;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import com.yjtc.cbg.commonutildemo.MainActivity;
import com.yjtc.cbg.commonutildemo.R;
import com.yjtc.cbg.commonutildemo.app.MyApplication;

/**
 * Created by chenboge on 16/8/2.
 * app启动页面设计和家在数据处理
 */
public class SplashActivity extends BaseActivity {

    private static final int SHOW_TIME_MIN = 3000;// 最小显示时间
    private long mStartTime;// 开始时间

    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case MyApplication.CITY_LIST_SCUESS:// 如果城市列表加载完毕，就发送此消息
                    long loadingTime = System.currentTimeMillis() - mStartTime;// 计算一下总共花费的时间
                    if (loadingTime < SHOW_TIME_MIN) {// 如果比最小显示时间还短，就延时进入MainActivity，否则直接进入
                        mHandler.postDelayed(goToMainActivity, SHOW_TIME_MIN
                                - loadingTime);
                    } else {
                        mHandler.post(goToMainActivity);
                    }
                    break;
                default:
                    break;
            }
        }
    };
    //进入下一个Activity
    Runnable goToMainActivity = new Runnable() {

        @Override
        public void run() {
            SplashActivity.this.startActivity(new Intent(SplashActivity.this,
                    MainActivity.class));
            finish();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aplash_layout);
        mStartTime = System.currentTimeMillis();//记录开始时间，
        MyApplication.getInstance().initData(mHandler);//开始加载数据
    }

    @Override
    public int getContentVIew() {
        return 0;
    }
}
